﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace UtilLib
{
    public static class DataUtil
    {
        /// <summary>
        /// Sets the data row value.
        /// </summary>
        /// <param name="dr">The dr.</param>
        /// <param name="ColumnName">Name of the column.</param>
        /// <param name="value">The value.</param>
        public static void SetDataRowValue(DataRow dr, string ColumnName, object value)
        {
            if (!dr.Table.Columns.Contains(ColumnName)) return;
            if (value == null)
                value = DBNull.Value;
            if (dr.HasVersion(DataRowVersion.Original) && dr.Table.GetChanges() == null)
            {
                if (!dr[ColumnName, DataRowVersion.Original].Equals(value))
                {
                    dr[ColumnName] = value;
                }
            }
            else
            {
                dr[ColumnName] = value;
            }
        }

        /// <summary>
        /// Sets the data row item array.
        /// </summary>
        /// <param name="drSource">The dr source.</param>
        /// <param name="drDestination">The dr destination.</param>
        public static void SetDataRowItemArray(DataRow drSource, DataRow drDestination)
        {
            SetDataRowItemArray(drSource, drDestination, null);
        }
        /// <summary>
        /// Sets the data row item array.
        /// </summary>
        /// <param name="drSource">The dr source.</param>
        /// <param name="drDestination">The dr destination.</param>
        public static void SetDataRowItemArray(DataRow drSource, DataRow drDestination, List<String> exceptColumns)
        {
            foreach (DataColumn col in drSource.Table.Columns)
            {
                if (exceptColumns != null && exceptColumns.Contains(col.ColumnName)) continue;
                if (drDestination.Table.Columns.Contains(col.ColumnName))
                {
                    drDestination[col.ColumnName] = drSource[col.ColumnName];
                }
            }
        }
    }
}
